import { getPermission } from '@/utils/auth'

export default{
  inserted(el, binding, vnode) {
    const { value } = binding
    const perms = getPermission()

    if (value && value instanceof Array && value.length > 0) {
      let hasPermission = false
      if (perms.indexOf('*') >= 0) {
        hasPermission = true
      } else {
        value.forEach(v => {
          hasPermission = perms.includes(v)
          if (hasPermission) {
            return
          }
        })
      }

      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else {
      throw new Error('权限必须是一个不为空的数组!')
    }
  }
}
